package dnl.util.ui;

import java.awt.Component;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JFileChooser;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author
 * @version 1.0
 */

public class GUIUtils {
	
	public static void setEnabledRecursively(Component component, boolean enabled){
		component.setEnabled(enabled);
		if(component instanceof JComponent){
			JComponent jc = (JComponent)component;
			Component[] containedComponents = jc.getComponents();
			for (int i = 0; i < containedComponents.length; i++) {
				
				setEnabledRecursively(containedComponents[i], enabled);
			}
		}
	}
	
	/**
	 * @param parent
	 * @param allowedExtensions
	 * @param extensionsDescriptor
	 * @param currentDir
	 * @return
	 */
	public static File chooseDirectory(Component parent, File currentDir, File selectedDir) {
		File[] result = chooseFileImpl(parent, JFileChooser.OPEN_DIALOG,
				JFileChooser.DIRECTORIES_ONLY, null, null, currentDir,
				selectedDir, true);
		if (result.length > 0)
			return result[0];
		return null;
	}
	/**
	 * @param parent
	 * @param allowedExtensions
	 * @param extensionsDescriptor
	 * @param currentDir
	 * @return
	 */
	public static File[] chooseFiles(Component parent, String[] allowedExtensions,
			String extensionsDescriptor, File currentDir) {
		return chooseFileImpl(parent, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY,
				allowedExtensions, extensionsDescriptor, currentDir, null, true);
	}
	/**
	 * @param parent
	 * @param allowedExtensions
	 * @param extensionsDescriptor
	 * @param currentDir
	 * @return
	 */
	public static File chooseFile(Component parent, String[] allowedExtensions,
			String extensionsDescriptor, File currentDir, File selectedFile) {
		File[] result = chooseFileImpl(parent, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY,
				allowedExtensions, extensionsDescriptor, currentDir, selectedFile, false);
		if (result != null && result.length > 0)
			return result[0];
		return null;
	}
	/**
	 * @param parent
	 * @param allowedExtensions
	 * @param extensionsDescriptor
	 * @param currentDir
	 * @return
	 */
	public static File chooseFileToSave(Component parent, String[] allowedExtensions,
			String extensionsDescriptor, File currentDir, File selectedFile) {
		File[] result = chooseFileImpl(parent, JFileChooser.SAVE_DIALOG, JFileChooser.FILES_ONLY,
				allowedExtensions, extensionsDescriptor, currentDir, selectedFile, false);
		if (result.length > 0)
			return result[0];
		return null;
	}
	public static JFileChooser getJFileChooser() {
		return fileChooser;
	}
	/**
	 * @param parent
	 *            the choosers parent frame.
	 * @param dialogType
	 *            JFileChooser.OPEN_DIALOG or JFileChooser.SAVE_DIALOG.
	 * @param selectionMode
	 *            JFileChooser.FILES_ONLY or JFileChooser.FILES_AND_DIRECTORIES
	 *            or JFileChooser.DIRECTORIES_ONLY.
	 * @param allowedExtensions
	 * @param extensionsDescriptor
	 * @param currentDir
	 * @param multiselectionEnabled
	 * @return
	 */
	private static File[] chooseFileImpl(Component parent, int dialogType, int selectionMode,
			String[] allowedExtensions, String extensionsDescriptor, File currentDir,
			File selectedFile, boolean multiselectionEnabled) {
		if (fileChooser == null)
			fileChooser = new JFileChooser(currentDir);
		fileChooser.setDialogType(dialogType);
		fileChooser.setMultiSelectionEnabled(multiselectionEnabled);
		fileChooser.setCurrentDirectory(currentDir);
		fileChooser.setFileSelectionMode(selectionMode);
		if (selectedFile != null) {
			fileChooser.setSelectedFile(selectedFile);
		}
		if (dialogType == JFileChooser.SAVE_DIALOG) {
			if (fileChooser.showSaveDialog(parent) != JFileChooser.APPROVE_OPTION) {
				// If user selected Cancel.
				return new File[0];
			}
		} else {
			if (fileChooser.showOpenDialog(parent) != JFileChooser.APPROVE_OPTION) {
				// If user selected Cancel.
				return new File[0];
			}
		}
		File[] files = null;
		if (multiselectionEnabled)
			files = fileChooser.getSelectedFiles();
		else {
			File sf;
			try {
				sf = fileChooser.getSelectedFile();
				if (sf == null)
					return new File[0];
				files = new File[]{sf};
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//fileChooser.setSelectedFile(null);
		return files;
	}
	//	private static JFileChooser getFileChooser() {
	//		synchronized (fileChooser) {
	//			if (fileChooser == null) {
	//				fileChooser = new JFileChooser();
	//				fileChooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
	//					public boolean accept(File f) {
	//						if (f.isDirectory()) {
	//							return true;
	//						}
	//						return extensionsFilter.accept(f);
	//					}
	//					public String getDescription() {
	//						return extensionsFilter.getDescription();
	//					}
	//				});
	//			}
	//		}
	//		return fileChooser;
	//	}
	private static JFileChooser fileChooser;
}